
#include "sceneTitle.h"
#include "../manager/gameMer.h"
#include "sceneMain.h"
#include <stdio.h>

void STG::sceneTitle::update(float deltaTime)
{
    time += deltaTime;
    if(time > 1.0f){
        time -= 1.0f;
    }
    //printf("deltatime:%f time: %f\n",deltaTime,time);
}

void STG::sceneTitle::init()
{
    bgm = Mix_LoadMUS("../assets/music/06_Battle_in_Space_Intro.ogg");
    if(bgm == nullptr)  
    {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Failed to load music: %s\n", Mix_GetError());
    }
    Mix_PlayMusic(bgm,-1);
}

void STG::sceneTitle::render()
{
    //渲染标题文字
    GameMer::instance()->renderTextCenter(std::string("太空大战"),0.2f,true);
    //渲染普通文字
    if(time < 0.5f){
        GameMer::instance()->renderTextCenter(std::string("按H进入游戏"),0.8f,false);
    }

}

void STG::sceneTitle::clean()
{
    if(bgm != nullptr)
    {
        Mix_HaltMusic();
        Mix_FreeMusic(bgm);
        bgm = nullptr;
    }
}

void STG::sceneTitle::handleEvents(SDL_Event *event)
{
    if(event->type == SDL_KEYDOWN)
    {
        if(event->key.keysym.scancode == SDL_SCANCODE_H)
        {
            scene* sence = new STG::sceneMain();
            GameMer::instance()->changeScene(sence);
        }
        if(event->type == SDL_KEYDOWN){
            if(event->key.keysym.sym == SDLK_ESCAPE){
                GameMer::instance()->overGame();
            }
        }
    }
}
